package com.lf.web.service.system.menu;

import com.lf.data.Cluster;
import com.lf.data.ClusterUtil;
import com.lf.data.Validate;
import com.lf.data.search.Field;
import com.lf.data.search.Query;
import com.lf.data.search.Sort;
import com.lf.web.service.ServiceSupport;
import org.apache.log4j.Logger;

import java.util.List;

public class MenuDetailService extends ServiceSupport {

    private static final Logger LOGGER = Logger.getLogger(MenuDetailService.class);
    private MenuDAO menuDAO;

    public void setMenuDAO(MenuDAO menuDAO) {
        this.menuDAO = menuDAO;
    }

    public String execute() {
        String id = form.getString("id");
        LOGGER.debug("查看菜单信息：id=" + id);
        if (Validate.check(Validate.OBJECTID, id)) {
            Cluster menu = menuDAO.findById(id);
            if (menu.isEmpty()) {
                LOGGER.error("所查看的菜单不存在");
                error(404);
                return NONE;
            }
            context.put("menu", menu);
            context.put("check", ClusterUtil.getCheck(Menu.class));
            findParents(menu);
            return "detail";
        } else {
            error(404);
            LOGGER.error("非法查看菜单信息：id=" + id);
            return NONE;
        }
    }

    private void findParents(Cluster menu) {
        Query query = new Query();
        query.in(Menu.ID, menu.getList(Menu.PARENT));
        Field field = new Field(Menu.NAME);
        Sort sort = new Sort();
        sort.asc(Menu.DEEP);
        List parents = menuDAO.find(query, field, sort);
        context.put("parents", parents);
    }

}
